package com.zjh.photoshopstudy.app;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.App;
import com.zjh.photoshopstudy.Utils.ExecutorUtil;
import com.zjh.photoshopstudy.Utils.GetNewComment;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;
import com.zjh.photoshopstudy.app.collection.CollectionActivity;
import com.zjh.photoshopstudy.app.download.DownloadActivity;
import com.zjh.photoshopstudy.app.download.DownloadingActivity;
import com.zjh.photoshopstudy.app.history.HistoryActivity;
import com.zjh.photoshopstudy.app.login.LoginActivity;
import com.zjh.photoshopstudy.app.main.MainFragment;
import com.zjh.photoshopstudy.app.upload.UploadActivity;
import com.zjh.photoshopstudy.app.upload.UploadingActivity;
import com.zjh.photoshopstudy.app.user.ChangePasswordActivity;
import com.zjh.photoshopstudy.app.user.CommentActivity;
import com.zjh.photoshopstudy.app.user.EditUserActivity;
import com.zjh.photoshopstudy.app.user.UserFragment;
import com.zjh.photoshopstudy.app.user.UserHttp;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_PERMISSION_STORAGE = 0x01;
    private ScheduledThreadPoolExecutor buzzerTimer;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private boolean isLogin = false;
    private boolean isInit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppUserEntity appUserEntity = SharePreferencesUtils.getLoginUser(this);
        if (appUserEntity != null) {
            isLogin = true;
        }
        initView();
    }

    private void initView() {
        mTabLayout = findViewById(R.id.tab);
        mViewPager = findViewById(R.id.main_pager);
        Fragment mMainFragment = new MainFragment();
        final List<Fragment> list = new ArrayList<>();
        list.add(mMainFragment);
        Fragment mUserFragment = new UserFragment();
        list.add(mUserFragment);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        mTabLayout.addTab(mTabLayout.newTab().setText("首页"));
        mTabLayout.addTab(mTabLayout.newTab().setText("我的"));
        mTabLayout.setupWithViewPager(mViewPager, false);
        mTabLayout.getTabAt(0).setText("首页");
        mTabLayout.getTabAt(1).setText("我的");
        checkSDCardPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isInit) {
            initGetComment();
        }
        isInit = true;
    }

    private void initGetComment() {
        buzzerTimer = ExecutorUtil.getInstance().newScheduledThreadPool(1);
        buzzerTimer.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                //执行事件
                int uid = (int) SharePreferencesUtils.getLoginUser(MainActivity.this).getId();
                UserHttp.getComment(uid);
            }
        }, 0, 1000*10, TimeUnit.MILLISECONDS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (buzzerTimer != null && !buzzerTimer.isShutdown()) {
            buzzerTimer.shutdown();
        }
    }

    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.history:
                startActivity(new Intent(this, HistoryActivity.class));
                break;
            case R.id.collection:
                intent = new Intent(this, CollectionActivity.class);
                intent.putExtra("type", "collection");
                startActivity(intent);
                break;
            case R.id.video_downloading:
                intent = new Intent(this, DownloadingActivity.class);
                intent.putExtra("type", "video");
                startActivity(intent);
                break;
            case R.id.materia_downloading:
                intent = new Intent(this, DownloadingActivity.class);
                intent.putExtra("type", "materia");
                startActivity(intent);
                break;
            case R.id.video_download:
                intent = new Intent(this, DownloadActivity.class);
                intent.putExtra("type", "video");
                startActivity(intent);
                break;
            case R.id.materia_download:
                intent = new Intent(this, DownloadActivity.class);
                intent.putExtra("type", "materia");
                startActivity(intent);
                break;
            case R.id.upload:
                intent = new Intent(this, UploadActivity.class);
                startActivity(intent);
                break;
            case R.id.uploading:
                intent = new Intent(this, UploadingActivity.class);
                startActivity(intent);
                break;
            case R.id.exitbtn:
                SharePreferencesUtils.clearLoginUser(this);
                intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case R.id.loginbtn:
                intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.reset_pwd:
                intent = new Intent(this, ChangePasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.edit_user:
                intent = new Intent(this, EditUserActivity.class);
                startActivity(intent);
                break;
            case R.id.course:
                intent = new Intent(this, CollectionActivity.class);
                intent.putExtra("type", "course");
                startActivity(intent);
                break;
            case R.id.comment:
                intent = new Intent(this, CommentActivity.class);
                startActivity(intent);
                break;
        }
    }

    protected void checkSDCardPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
        }
    }
}
